﻿namespace EasyGo.Data.QueryServices
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using EasyGo.Common.Dto;
    using EasyGo.Common.Queries;
    using EasyGo.Logic.Model;

    public class CustomerQueries : ICustomerQueries
    {
         private readonly ApplicationDbContext context;

        public CustomerQueries(ApplicationDbContext context)
        {
            this.context = context;

        }

        public IEnumerable<CustomerDto> GetAllCustomers()
        {
            return
                this.context.Customers.Select(
                    x => new CustomerDto() { Id = x.Id, Name = x.Name, CreateDate = x.AddDate });
        }

        public CustomerDto FindCustomer(int id)
        {
            return
                this.context.Customers.Select(
                    x => new CustomerDto() { Id = x.Id, Description = x.Description, Name = x.Name, })
                    .SingleOrDefault(x => x.Id == id);
        }

        public IEnumerable<LookupDto> GetLookup(string term)
        {
            IQueryable<Customer> query = this.context.Customers.OrderByDescending(x => x.AddDate);
            if (!string.IsNullOrEmpty(term))
            {
                query = query.Where(x => x.Name.StartsWith(term));
            }

            return query.Take(10).Select(x => new LookupDto { Id = x.Id, Text = x.Name });
        }
    }
}
